var cidchecked=0;
function getId(id)
{
	return document.getElementById(id);
}
function submitform(arg)
{
    if(arg==undefined)
    {
        arg.act='';
        arg.ajax=false;
        arg.goback=false;
    }
    if(arg && arg.act && arg.act!='')
    {
        document.form_admin.action=arg.act;
    }
	if (typeof document.form_admin.onsubmit == "function") {

		if(document.form_admin.onsubmit()==false)
        {
            return false;
        }
	}
    if(arg && arg.ajax==true)
    {
        AIM.submit(document.form_admin,null,arg.goback);
    }
	document.form_admin.submit();

}
function checkall(cb)
{
    cidchecked=0;
    for(i in $ES(".cid"))
    {
        var elob=$ES(".cid")[i];
        elob.checked=cb.checked;
        if(cb.checked==true)
        {
            cidchecked++;
        }
    }
}
function isChecked()
{
    cidchecked=0;
    for(i in $ES(".cid"))
    {
        var elob=$ES(".cid")[i];
        if(elob.checked==true)
        {
            cidchecked++;
        }
    }
    if(cidchecked>0)
    {
        return true;
    }
    return false;
}
function showhide(id)
{
	obj=getId(id);
	if(obj.style.display=='none')
	{
		obj.style.display='';
	}else
	{
		obj.style.display='none';
	}
}
function GetXmlHttpObject()
{
    var xmlHttp = null;
    try {
        xmlHttp = new XMLHttpRequest()
    }
    catch (e)
    {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request")
    }
    return xmlHttp
}
function ajaxLoader(url, post, loader, alertdiv, func,img)
{
    http = GetXmlHttpObject();
    if (post == "") {
        http.open("GET", url, true)
    }
    else
    {
        http.open("POST", url, true);
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    }
    http.onreadystatechange = function ()
    {
        if (http.readyState == 1)
        {
            try
            {
				if(img==undefined)img="styles/loading2.gif";
                getId(alertdiv).innerHTML = '<img src="'+img+'" border=0/>'
            }
            catch (e) {}
        }
        if (http.readyState == 4)
        {
            var datares = http.responseText;
            try {
                getId(alertdiv).innerHTML = ''
            }
            catch (e) {}
            try {
                eval(func + "(datares)")
            }
            catch (e) {}
            try {
                getId(loader).innerHTML = datares
            }
            catch (e) {}
            try
            {
                arrscr = (datares).split("<script>");
                arrscr = (arrscr[1]).split("</script>");
                eval(arrscr[0])
            }
            catch (e) {}
        }
    };
    http.send(post)
}
function OpenPopup(url, w,h) {
	opener = window.open(url,popupName,'scrollbars=yes,menubar=no,status=yes,resizeble=yes,width='+w+',height='+h);
}
function openPI(title,url,w,h) {
    var b=new TrDialog;
    if(w==0 || h==0)
    {
        sizep=b.getSizePage();
        w=sizep.w-80;
        h=sizep.h-50;
    }
    b.init({width:w, height:h,resize:true,model:true});
    b.setTitle(title);
    b.setUrl(url);
    b.show();
}
function uploadImg(id,fol,val,foldf)
{
	if(val==undefined ||val=="")getId(id).value="";
	var stid=(val!=undefined && val.length>0)?'':"display:none";
	document.write('<span id="divimg_'+id+'" style="'+stid+'"> <img style="cursor: pointer;" onclick="openPI(\'file\',this.src,400,300)" src="'+fol+val+'" id="ImgV_'+id+'" width="20" height="20" /> ');
	document.write(' <img src="styles/del.png" style="cursor:pointer;" onclick="getId(\''+id+'\').value=\'\';getId(\'divimg_'+id+'\').style.display=\'none\'" /></span>');
	document.write(' <input type="button" onclick="tinyMCE.execCommand(\'mceTrImgC\',{t: \''+id+'\',func:fuc_upload_pr,fdf: \''+foldf+'\',m:false})" value="Choose Image" class="b1"/> ');

}
function fuc_upload_pr(obj)
{
	getId(obj.t).value=obj.p+obj.f;
	getId("divimg_"+obj.t).style.display='';
	getId("ImgV_"+obj.t).src=obj.r+obj.p+obj.f;
}
function PopUpMove(id,pa,e)
{
	try{
		objm=document.getElementById("div_move_page_row");
		if(objm)document.body.removeChild(objm);
	}catch(er){}
	var new_div=document.createElement("div");
	new_div.id="div_move_page_row";
	new_div.innerHTML='<li><a href=# onclick="location=location.href+\'&move=backpage&idmove='+id+'\'">Move to Back page</a></li>';
	new_div.innerHTML+='<li><a href=# onclick="location=location.href+\'&move=backrow&idmove='+id+'\'">Move to Back Row</a></li>';
	new_div.innerHTML+='<li><a href=# onclick="location=location.href+\'&move=nextrow&idmove='+id+'\'">Move to Next Row</a></li>';
	new_div.innerHTML+='<li><a href=# onclick="location=location.href+\'&move=nextpage&idmove='+id+'\'">Move to Next page</a></li>';
	new_div.style.position="absolute";
	new_div.style.background="#fff";
	new_div.style.padding="10px";
	var IE = document.all? true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE);
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY ;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }
	new_div.style.left= tempX+"px";
	new_div.style.top= tempY+"px";
	new_div.onmouseout=pa.onmouseout=function(){new_div.to=setTimeout(function(){document.body.removeChild(new_div)},500)};
	new_div.onmouseover=function(){clearTimeout(new_div.to)};
	document.body.appendChild(new_div);
}

AIM = {
    inopt : 0,
    in_ajax:0,
    timealert_down: 0,
    timealert_up:0,
    divalert: document.createElement("div"),
	frame : function(c) {

		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);

		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
		return n;
	},

	form : function(f, name) {
		f.setAttribute('target', name);
		this.in_ajax=document.createElement("input");
        this.inopt=document.createElement("input");
		this.in_ajax.type="hidden";
		this.in_ajax.name="AjaxFrame";
		this.in_ajax.value="true";
        this.inopt.type="hidden";
        this.inopt.name="AjaxOpt";
        this.inopt.value=this.o;
		f.appendChild(this.in_ajax);
        f.appendChild(this.inopt);
	},

	submit : function(f, c, o) {
        this.f=f;
		this.o=o;
		this.form(f, AIM.frame(c));
		this.showProgress("Saving...",{img:true});
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id) {
		this.hideProgress();
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}
		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML,AIM);
		}
		arr=(d.body.innerHTML).split("(*_*)");
		var alertt="<b>"+arr[2]+"</b>";
		if(arr[1]=="ok")
		{
			if(arr[4]=="reset")
			{
				this.showProgress(alertt,{time:1,timeclose:10000});
				this.f.reset();
				return;
			}else
			if(AIM.o===true)
			{
				arr[3]=arr[3].replace(/&amp;/g,"&");
				this.showProgress(alertt,{time:1,actionclose:'location="'+arr[3]+'"',timeclose:3000});
				return;
			}
            else if(AIM.o==1)
            {
                this.f.reset();
            }else if(AIM.o==2)
            {
                this.showMsgAlert(alertt);
                this.f.reset();
                return;
            }else if(AIM.o==3 && arr[3]!=undefined)
            {
                arr[3]=arr[3].replace(/&amp;/g,"&");
                location=arr[3];
            }else if(AIM.o==4)
            {
                this.showMsgAlert(alertt);
                return;
            }
			this.showProgress(alertt,{time:1,timeclose:3000});
		}else if(arr[2]!=undefined)
		{
			this.showProgress(alertt,{time:1});
		}else
		{
			this.showProgress(d.body.innerHTML,{time:1});
		}
	},
	hideProgress : function(msg,p) {
	   try{
		document.body.removeChild(document.getElementById('drootvlo'));
		clearTimeout(this.tm1);
		clearTimeout(this.tm2);
        }catch(err){}
	},
    showMsgAlert : function (msg)
    {
        var newdivfix=getId('MsgAlert');
        var coord=this.GetCoords();
        newdivfix.className="msgalertfix";
        AIM.divalert.className="msgalert";
        AIM.divalert.innerHTML=msg;
        AIM.divalert.style.marginTop=coord.y-100;
        AIM.divalert.top=coord.y-100;
        newdivfix.appendChild(AIM.divalert);
        clearInterval(AIM.timealert_up);
        AIM.timealert_up=0;
        clearInterval(AIM.timealert_down);
        AIM.timealert_down=0;
        AIM.timealert_down=setInterval(function(){
            if(AIM.divalert.top<coord.y){
                AIM.divalert.top+=2;
                AIM.divalert.style.marginTop=AIM.divalert.top;
            }else{
                clearInterval(AIM.timealert_down);
                AIM.timealert_down=0;
            }
        },10);
        document.onmousedown=window.onscroll=function(e)
        {
            if(AIM.timealert_up==0)
            {
                AIM.timealert_up=setInterval(function(){
                    if(AIM.divalert.top>-120 && AIM.timealert_down==0){
                        AIM.divalert.top-=3;
                        AIM.divalert.style.marginTop=AIM.divalert.top;
                    }else{
                        newdivfix.className="";
                        clearInterval(AIM.timealert_up);
                        AIM.timealert_up=0;
                        newdivfix.removeChild(AIM.divalert);
                    }
                },10);
            }
        };
    },
	showProgress : function(msg,p) {
		var self=this;
		img=p.img;
		time=p.time;
		timeclose=p.timeclose;
		actionclose=p.actionclose;

		var new_div_root=document.createElement("div");
		new_div_root.setAttribute("id","drootvlo");

		var new_div_visi=document.createElement("div");
        var pages=this.getSizePage();
		new_div_visi.className="divpageout";
		new_div_visi.style.width="100%";
		new_div_visi.style.height=pages.h;

		if (document.documentElement && document.documentElement.scrollTop)
			var pY =  document.documentElement.scrollTop;
		else if (document.body)
			var pY =  document.body.scrollTop;
		var new_loader=document.createElement("div");
		new_loader.className="divloader";
		new_loader.style.top=document.body.clientHeight/2 +pY-100;
		new_loader.style.left=document.body.clientWidth/2-120;

		if(img==true)new_loader.innerHTML+="<img src='styles/loading2.gif'>";
		new_loader.innerHTML+="<div style='padding-top:10px'>"+msg+"</div><div id='spidivmsg'></div>";

		var funclose=function(){self.hideProgress(); if(actionclose!=undefined)eval(actionclose)};
		var new_button_close=document.createElement("a");
		new_button_close.style.cursor="pointer";
		new_button_close.innerHTML="[ Close ]";
		new_button_close.onclick=funclose;
		new_div_root.appendChild(new_div_visi);
		new_div_root.appendChild(new_loader);
		document.body.appendChild(new_div_root);

		if(time==undefined)
		{
			time=20000;
		}
		this.tm1=setTimeout(function(){new_loader.appendChild(new_button_close);new_div_visi.onclick=funclose},time);
		if(timeclose>1)
		{
			this.tm2=setTimeout(function(){self.hideProgress();if(actionclose!=undefined)eval(actionclose)},timeclose);
		}
	},
    GetCoords :function()
    {
        var mysize=new Object;
        if (document.all)
        {
            if (document.documentElement.scrollLeft>0)
                mysize.x = document.body.scrollLeft;
            else
                mysize.x = document.documentElement.scrollLeft;

            if (!document.documentElement.scrollTop>0)
                mysize.y = document.body.scrollTop;
            else
                mysize.y = document.documentElement.scrollTop;
        }
        else
        {
            mysize.x = window.pageXOffset;
            mysize.y = window.pageYOffset;
        }
        return mysize;
    },
     getSizePage:function ()
    {
        var mysize=new Object;
        if(window.innerHeight) {
            // all except explorer
            mysize.w = window.innerWidth;
            mysize.h = window.innerHeight;
        } else if(document.documentElement && document.documentElement.clientHeight) {
            // explorer 6 strict mode
            mysize.w = document.documentElement.clientWidth;
            mysize.h = document.documentElement.clientHeight;
        } else if(document.body) {
            // other explorers
            mysize.w = document.body.clientWidth;
            mysize.h = document.body.clientHeight;
        };
        return mysize;
    }
};